<template>
  <div class="ranking page">
    <div class="ranking-header">
      <img src="../assets/ranking-header.jpg" alt="">
      <button @click="goUser">返回</button>
      <div class="bonus">
        <span>我的奖金:{{ fenToYuan(personalRankInfo.rewardValue)  || 0 }}</span>
        <span>排行第{{ personalRankInfo.rewardPosition || 0 }}位</span>
      </div>
      <div class="tab">
        <div @click="clickMoneyList" :class="[isActive ? 'un-activate': 'activate']"  class="money">
           <span>奖金榜</span>
        </div>
        <div @click="clickPropagationList" :class="[isActive ? 'activate': 'un-activate']"  class="spread">
          <span>推广榜</span>
        </div>
      </div>
    </div>
    <div class="ranking-content-top">
      <span>排行</span>
      <span>用户</span>
      <span v-if="!isActive">获得的奖金</span>
      <span v-if="isActive">扫码人数</span>
    </div>
    <ul class="ranking-content">
      <div v-if="!rankList.length" class="no-data">
        <span >暂无数据</span>
      </div>
      <li v-for="item in rankList">
        <span v-if="item.sequence>3">{{ item.sequence }}</span>
        <img v-if="Number(item.sequence)===1" src="../assets/gold-medal.png" alt="">
        <img v-if="Number(item.sequence)===2" src="../assets/silver-medal.png" alt="">
        <img v-if="Number(item.sequence)===3" src="../assets/copper-medal.png" alt="">
        <span>{{ hideName(item.displayName) }}</span>
        <span v-if="item.sharedCount">{{ item.sharedCount }}</span>
        <span v-if="item.total">{{ fenToYuan(item.total) }}</span>
      </li>
    </ul>
    <div class="ranking-footer">
      <img src="../assets/ranking-footer.png" alt="">
    </div>
  </div>
</template>
<script>
  import { getRewardList, getPropagationList } from '../api/user';
  export default { data() {
      return {
        isActive: false,
        rankList: [],
        personalRankInfo: {
          rewardValue: window._META_INFO.rewardValue,
          sharedCount: window._META_INFO.sharedCount
        }
      };
    },
    methods: {
      clickMoneyList() {
        this.isActive = false;
        this.getRankData();
      },
      fenToYuan(fen) {
        return parseInt(fen / 100, 10);
      },
      hideName(name) {
        if (name.length === 1) {
          return name;
        }
        return name.slice(0, 1) + '*' + name.slice(-1);
      },
      async clickPropagationList() {
        this.isActive = true;
        const result = await getPropagationList();
        this.rankList = result.data.rankingList;
      },
      goUser() {
        this.$router.push('/user');
      },
      async getRankData() {
        const rewardInfo = await getRewardList();
        let pos = rewardInfo.data.position;
        const rankItem = rewardInfo.data.rankingList.find(
          rank => rank.userId === window._META_INFO.user.userLoginId
        );
        // 如果用户在排行榜，取排行榜里面的位置
        pos = rankItem ? rankItem.sequence : pos;
        window._META_INFO = Object.assign(window._META_INFO,
          { rewardValue: rewardInfo.data.rewardValue },
          { rewardPosition: pos });
        this.personalRankInfo = {
          rewardValue: rewardInfo.data.rewardValue,
          rewardPosition: pos
        };
        this.rankList = rewardInfo.data.rankingList;
      }
    },
    created() {
      this.getRankData();
    }
  };
</script>
<style scoped>
  ul, li {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .no-data {
    height: 100%;
    width: 100%;
    font-size: 0.35rem;
    color: #c0c0c0;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .page {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .ranking-header{
    font-size: 0;
    height: 3.2rem;
    position: relative;
    width: 100%;
  }
  .ranking-header img{
    padding: 0;
    margin: 0;
    font-size: 0;
    border: none;
    height: 100%;
    width: 100%;
  }
  .ranking-header button{
    position: absolute;
    height: 0.45rem;
    width: 1.57rem;
    border-radius: 0.2rem;
    right: 0.2rem;
    top: 0.2rem;
    color: #ffffff;
    font-size: 0.28rem;
    background-color: #000000;
    opacity: 0.4;
    outline: none;
    border-width:0;
   }
  .bonus {
    font-size: 0.44rem;
    color: #ffffff;
    height: 0.5rem;
    width: 100%;
    position: absolute;
    bottom: 0.9rem;
    display: flex;
    justify-content: center;
  }
  .bonus span:nth-child(1){
    margin-right: 0.3rem;
  }
  .tab {
    position: absolute;
    bottom: 0;
    left: calc(50% - 2.5rem);
    /*padding: 0 0.2rem;*/
    font-size: 0.34rem;
  }
  .spread,.money {
    float: left;
    height: 0.7rem;
    line-height: 0.8rem;
    text-align: center;
    border-top-left-radius: 1.2em;
    border-top-right-radius: 1.2em;
  }
  .spread {
    width: 2.6rem;
  }
  .money {
    width: 2rem;
  }
  .un-activate {
    color: #ffffff;
    background-image: linear-gradient(to right, #3BC7DD, #578DE4);
  }
  .activate {
    color: #5bB6e5;
    background-color: #ffffff;
  }
  .ranking-content-top {
    position: relative;
    line-height: 0.9rem;
    height: 0.9rem;
    color: #ffffff;
    font-size: 0.36rem;
    background-image:linear-gradient(to right,#578DE4,#3BC7DD);
  }
  .ranking-content-top span:nth-child(1) {
    position: absolute;
    left: 12%;
  }
  .ranking-content-top span:nth-child(2) {
    position: absolute;
    left: 42%;
  }
  .ranking-content-top span:nth-child(3) {
    position: absolute;
    left: 70%;
  }
  .ranking-content li {
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    align-items: center;
    line-height: 1.4rem;
    height: 1.4rem;
    border-bottom: 1px solid #c0c0c0;
  }
  .ranking-content {
    position: relative;
    flex: 1;
    overflow:scroll;
  }
  .ranking-content li span{
    line-height: 1.4rem;
    font-size: 0.28rem;
    color: #282828;
  }
  .ranking-content li img{
    height: 0.7rem;
    width: 0.5rem;
    position: absolute;
    padding-top: 4%;
    align-self: flex-start;
    left: 12%;
  }
  .ranking-content li span:nth-child(1) {
    position: absolute;
    left: 14%;
  }
  .ranking-content li span:nth-child(2) {
    width: 1.6rem;
    position: absolute;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
    left: 40%;
  }
  .ranking-content li span:nth-child(3) {
    position: absolute;
    left: 73%;
  }
  .ranking-footer{
    font-size: 0;
    bottom: 0;
  }
  .ranking-footer img{
    width: 100%;
  }
</style>
